<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/31
 * Time: 9:25
 */

namespace Api\Controller;

use Think\Controller;

class LoginController extends Controller
{
    public function login($username, $password)
    {
        $where ["password"] = strip_tags(md5($password));
        $where ["username"] = strip_tags($username);
        $result = M("Admin")->where($where)->find();
        if ($result) {
            return $result;
        }
    }

    /**
     * 注册
     */
    public function register()
    {
        $mobile = I('request.mobile');
        $data['mobile'] = $mobile;
        $code = I('request.code');
        $data['password'] = md5(I('request.password'));
        $openid = I('request.openId');
        if ($mobile && $code) {
            $sql_code = M('sms')->where("mobile='$mobile' and state=2")->order('id desc')->field(array('code', 'state', 'id'))->find();
            $code_id = $sql_code['id'];
            if ($code == $sql_code['code']) {
                $result_user = M('user')->where("openid='$openid'")->find();
                if (empty($result_user['mobile'])) {
                    if ($result_user['id']) {
                        $id = $result_user['id'];
                        $result = M('user')->where("id='$id'")->save($data);
//$this->ajaxreturn(M()->_sql());
                        if ($result !== false) {
                            M('sms')->where("id='$code_id'")->setField('state', 1);
                            $this->ajaxreturn(array('state' => 1), 'json');
                        } else {
                            //注册失败
                            $this->ajaxreturn(array('state' => -1), 'json');
                        }
                    } else {
                        //已经注册过
                        $this->ajaxreturn(array('state' => -1), 'json');
                    }
                } else {
                    //没有根据openid找到人
                    $this->ajaxreturn(array('state' => -3), 'json');
                }

            } else {
                //验证码错误
                $this->ajaxreturn(array('state' => 0), 'json');
            }
        } else {
            //没有输入手机号或者验证码
            $this->ajaxreturn(array('state' => -2), 'json');
        }

    }

    /**
     * 前端登录(实质是绑定功能)
     */
    public function onstageLogin()
    {
        $openid = $this->get_openid();

        if (empty($openid)) {
            $openid = I('request.user_id');
        }
        $mobile = I('request.mobile');
        $code = I('request.code');
        //$data['password'] = md5(I('request.password'));
        $data['mobile'] = I('request.mobile');
        $sql_code = M('sms')->where("mobile='$mobile' and state=2")->order('id desc')->field(array('code', 'state', 'id'))->find();
        $code_id = $sql_code['id'];
        if ($code && $mobile) {
            if ($code == $sql_code['code']) {
                $result = M('user')->where("openid='$openid'")->save($data);
                M('sms')->where("id='$code_id'")->setField('state', 1);
            } else {
                //验证码失效或者验证码错误
                $this->ajaxreturn(array('state' => -3), 'json');
            }
            if ($result === false) {
                //绑定失败
                $this->ajaxreturn(array('state' => -1), 'json');
            } else {
                //绑定成功
                $this->ajaxreturn(array('state' => 1), 'json');
            }
        } else {
            //没有输入手机号或者验证码
            $this->ajaxreturn(array('state' => -2), 'json');
        }

    }

    /**
     * 是否绑定了手机号
     */
    public function bindMobile()
    {
        $openid = I('post.openid');
        //判断您是否绑定了手机号
        $mobile = M('user')->where("openid='$openid'")->getField('mobile');
        if (empty($mobile)) {
            $this->ajaxreturn(array('state' => 0), 'json');
        } else {
            $this->ajaxreturn(array('state' => 1, 'data' => $mobile), 'json');
        }
    }

    /**
     * 自动登录
     */
    public function automaticLogin()
    {
        $where['openid'] = I('request.openid');
        $where['mobile'] = I('request.mobile');
        $where['frozen']=2;
        $result = M('user')->where($where)->find();
        if ($result) {
            $this->ajaxreturn(array('state' => 1), 'json');
        } else {
            $this->ajaxreturn(array('state' => 0), 'json');
        }
    }
}
